#include <iostream>
#include <vector>
using namespace std;

class Solution {
public:
    // 322. 零钱兑换
    // https://leetcode.cn/problems/coin-change/
    int coinChange(vector<int>& coins, int amount) {
        int n = coins.size();
        const static int INF = 0x5f5f5f5f;
        vector<int> dp(amount+1);

        for (int j = 1; j<=amount; ++j) 
            dp[j] = INF;
        
        for (int i = 1; i<=n; ++i)
        {
            for (int j = coins[i-1]; j<=amount; ++j)
            {
                if (dp[j-coins[i-1]] != INF)
                    dp[j] = min(dp[j-coins[i-1]] + 1, dp[j]);
            }
        }

        return dp[amount] == INF ? -1 : dp[amount];
    }
};